package com.jianyun.jyzs.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.FlowExamineActivity;
import com.jianyun.jyzs.utils.WorkFlowActHelper;


@SuppressLint("ViewConstructor")
public class SearchPopWindow extends PopupWindow implements OnClickListener{
	
	private FlowExamineActivity act;
	public View conentView;
    public EditText searchTitle;
    public EditText searchWork;
    public TextView searchHead;
    public ImageView searchHeadCancal ;
    public TextView searchTime ;
    public ImageView searchTimeSelect ;
    public Button cancelButton ;
    public Button searchButton ;
   
	@SuppressLint("InflateParams")
	public SearchPopWindow(FlowExamineActivity act, int layout, String[] data) {
		 LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        conentView = inflater.inflate(layout, null);//R.layout.popupwindow_add
	        this.act = act;
	        // 设置SelectPicPopupWindow的View
	        this.setContentView(conentView);
	        // 设置SelectPicPopupWindow弹出窗体的宽
	        this.setWidth(LayoutParams.WRAP_CONTENT);
	        // 设置SelectPicPopupWindow弹出窗体的高
	        this.setHeight(LayoutParams.WRAP_CONTENT);
	        // 设置SelectPicPopupWindow弹出窗体可点击
	        this.setFocusable(true);
	        this.setOutsideTouchable(true);
	        // 刷新状态
	        this.update();
	        // 实例化一个ColorDrawable颜色为半透明
	        ColorDrawable dw = new ColorDrawable(0000000000);
	        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
	        this.setBackgroundDrawable(dw);
	        
	        // 设置SelectPicPopupWindow弹出窗体动画效果
	        this.setAnimationStyle(R.style.AnimationPreview);

	        searchTitle = (EditText)conentView.findViewById(R.id.searchTitle);
	        searchWork = (EditText)conentView.findViewById(R.id.searchWork);
	        searchHead = (TextView)conentView.findViewById(R.id.searchHead);
	        searchHeadCancal = (ImageView)conentView.findViewById(R.id.searchHeadCancal);
	        searchTime = (TextView)conentView.findViewById(R.id.searchTime);
	        searchTimeSelect = (ImageView)conentView.findViewById(R.id.searchTimeSelect);
	        cancelButton = (Button)conentView.findViewById(R.id.cancelButton);
	        searchButton = (Button)conentView.findViewById(R.id.searchButton);

	        if(data != null && data.length == 4){
	        	if(data[0] != null){
	        		searchTitle.setText(data[0]);
	        	}
	        	if(data[1] != null && !data[1].trim().equals("")){
	        		searchWork.setText(data[1]);
	        	}
	        	if(data[2] != null){
	        		searchHead.setText(data[2]);
	        	}
	        	if(data[3] != null){
	        		searchTime.setText(data[3]);
	        	}
	        }
			searchTime.setOnClickListener(this);
			searchTimeSelect.setOnClickListener(this);
		    searchHead.setOnClickListener(this);
		    searchHeadCancal.setOnClickListener(this);
			searchButton.setOnClickListener(this);
			cancelButton.setOnClickListener(this);
	    }
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.searchTime:
				WorkFlowActHelper.showDate(act);
				break;
			case R.id.searchTimeSelect:
				WorkFlowActHelper.showDate(act);
				break;
			case R.id.searchHead:
				WorkFlowActHelper.showPp(act,searchHead);
				break;
			case R.id.searchHeadCancal:
				searchHead.setText("");
				break;
			case R.id.searchButton:
				String[] data = new String[4];
				if(searchTitle.getText() != null && !searchTitle.getText().toString().trim().equals("")){
					data[0] = searchTitle.getText().toString();
				}else{
					data[0] = null;
				}
				if(searchWork.getText() != null && !searchWork.getText().toString().trim().equals("")){
					data[1] = searchWork.getText().toString();
				}else{
					data[1] = null;
				}
				if(searchHead.getText() != null && !searchHead.getText().toString().trim().equals("")){
					data[2] = searchHead.getText().toString();
				}else{
					data[2] = null;
				}
				if(searchTime.getText() != null && !searchTime.getText().toString().trim().equals("")){
					data[3] = searchTime.getText().toString();
				}else{
					data[3] = null;
				}
			switch (act.currIndex) {
			case 0://
				act.waitDoFragment.data = data;
				break;
			case 1://
				act.havaDoFragment.data = data;
				break;
			case 2://
				act.shareTaskFragment.data = data;
				break;
			default:
				break;
			}
			act.searchData();
            dismiss();	
		break;
		case R.id.cancelButton:
			switch (act.currIndex) {
			case 0://
				act.waitDoFragment.data = null;
				break;
			case 1://
				act.havaDoFragment.data = null;
				break;
			case 2://
				act.shareTaskFragment.data = null;
				break;
			default:
				break;
			}
//            act.searchData();
            dismiss();	
		break;
		default:
			break;
		}
	}

    /**
     * 显示popupWindow
     * 
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, 0, 0);
        } else {
            this.dismiss();
        }
    }

}
